<?php
namespace app\admin\model\Auth;
use support\exception\Exception;
use support\Model;
use think\model\concern\SoftDelete;

class MenuModel extends Model
{
    use SoftDelete;

    protected $name = "menu";

    protected $pk = "menu_id";

    protected $deleteTime = 'deleted_at';


    /**
     * 是否启用:0=否,1=是
     */
    const IS_STATUS_FALSE = 0;
    const IS_STATUS_TRUE = 1;


    /**
     * 获取列表
     * @param array $option
     * @param array|string[] $field
     * @param string $sortName
     * @param string $sortType
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLists($option = [] , array $field = ['*'] , string $sortName = "menu_id" , string $sortType = "desc" )
    {
        return $this->where($option)->order($sortName , $sortType)->select();
    }

    /**
     * 获取子菜单
     * @param array $option
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChild( array $option = [])
    {
        $ret = $this->getLists($option);
        if(!empty($ret)){
            foreach ($ret as $key => &$item){
                $item = $item->toArray();
                $item['child'] = $this->getChild( [ 'pid' => $item['menu_id'] ]);
            }
        }
        return $ret;
    }

    /**
     * 递归删除菜单
     * @param array $menuIds
     * @return bool
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
//    public function deleteRecord($option) : bool
//    {
//        $where = [];
//        $where = array_merge($where , $option);
//        $list = $this->where($where)->select();
//        $childrenIds = [];
//        if(!$list->isEmpty()){
//            foreach($list as $v){
//                $childrenIds[] = $v->menu_id;
//                $delRes = $v->delete();
//                if($delRes === false){
//                    throw new Exception("删除失败");
//                }
//            }
//            if(!empty($childrenIds)){
//                $this->deleteRecord(['pid' => $childrenIds]);
//            }
//        }
//        return true;
//    }


}